#include<memory>
#include"Socket.hpp"
#include"Protocol.hpp"
#include<iostream>
#include<string>
#include"Common.hpp"

using namespace SocketModule;

void Usage(std::string proc)
{
    std::cerr<<"Usage: "<< proc << " port" << std::endl;
}

void GetDataFromStdin(int *x, int *y, char *oper)
{
    std::cout << "Please Enter x: ";
    std::cin >> *x;
    std::cout << "Please Enter y: ";
    std::cin >> *y;
    std::cout << "Please Enter oper: ";
    std::cin >> *oper;
}

int main(int argc, char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }   
    std::string server_ip = argv[1];
    uint16_t server_port = std::stoi(argv[2]);

    std:;std::shared_ptr<Socket> client = std::make_unique<TcpSocket>();
    client->BuildTcpClientSocketMethod();

    if(client->Connect(server_ip, server_port) != 0)
    {
        std::cerr<<"connect error" << std::endl;
        exit(CONNECT_ERR);
    }
    std::unique_ptr<ProtoCal> protocol = std::make_unique<ProtoCal>();
    std::string resp_buffer;

    while(1)
    {
        int x, y;
        char oper;
        GetDataFromStdin(&x, &y, &oper);

        std::string req_str = protocol->BuildRequestString(x, y, oper);

        client->Send(req_str);

        Response resp;
        bool res = protocol->GetResponse(client, resp_buffer, &resp);

        if(res == false)
        break;

        resp.ShowResult();
    }
    client->Close();
    return 0;
}

